<template>
    <div class="goods_cate">
 <!-- 面包屑组件 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

     <el-button type="primary" plain>添加分类</el-button>

     <!-- treeStructure是否树形结构
     columns 列的属性
     dataSource 数据源
     controls 是否显示操作列 -->
     <tree-grid
      class="mt-15"
      :treeStructure="true"
      :columns="columns"
      :data-source="dataSource"
      @deleteCate="deleteCategory"
      @editCate="editCategory"
    >

    </tree-grid>
<!-- 分页组件 -->
    <el-pagination
      class="mt-15 page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="1"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
</template>
<script>
import { getCate } from '@/api'
// 1. 引入TreeGrid组件
import TreeGrid from '@/components/TreeGrid/TreeGrid'
export default {
  components: {
    TreeGrid
  },
  data () {
    return {
      total: 0, // 记录数据总条数
      pagesize: 10, // 每页多少条，默认2条
      pagenum: 1, // 当前页面，默认第一页

      dataSource: [],
      columns: [{
        text: '分类名称',
        dataIndex: 'cat_name',
        width: ''
      }, {
        text: '是否有效',
        dataIndex: 'cat_deleted',
        width: ''
      }, {
        text: '排序',
        dataIndex: 'cat_level',
        width: ''
      }]
    }
  },
  created () {
    this.initTable()
  },
  methods: {
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.initTable()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.initTable()
    },
    // 删除分类
    deleteCategory (cid) {
      console.log(cid)
    },
    // 编辑分类
    editCategory (cid) {
      console.log(cid)
    },
    initTable () {
      getCate(3, this.pagesize, this.pagenum)
        .then(res => {
        //   console.log(res)
          this.total = res.data.total
          this.dataSource = res.data.result
        })
    }
  }
}
</script>
